<?php
define( 'DS' , DIRECTORY_SEPARATOR );
// define( 'APP_ROOT' , dirname( dirname( __FILE__ ) ) . DS  );
define( 'APP_ROOT' , dirname( __FILE__ ) . DS  );

date_default_timezone_set("Asia/Shanghai");
$action = empty( $_REQUEST['ajax'] ) ? '' : strtolower( $_REQUEST['ajax'] );
if($action){
	if($action=='js'){
		$ContentType ='application/x-javascript';
	}else{
		$ContentType ='text/plain';
	}
}else{
	$ContentType ='text/html';
}
header("Content-Type:".$ContentType.";charset=utf-8");

// define( 'KIS_APP_ROOT' , dirname( APP_ROOT ) . DS );
define( 'KIS_APP_ROOT' , APP_ROOT . DS .'php'.DS);
// include_once KIS_APP_ROOT.'ext_lib'. DS.'comm.function.php';
define( 'AROOT' , KIS_APP_ROOT);
define( 'CROOT' , KIS_APP_ROOT.'_lp'. DS.'core'. DS  );
include_once CROOT. 'lib'.DS.'core.function.php';
// include_once( KIS_APP_ROOT. 'ext_lib'.DS .'mail.config.inc.php' );
include_once KIS_APP_ROOT.'lib'. DS.'app.function.php';

include_once 'php/map.inc.php';
include_once 'php/func.inc.php';

define('TBS_ROOT', KIS_APP_ROOT.'_lp'.DS );
define('TPLROOT', dirname( __FILE__ ) . DS. 'html'.DS );

define('APP_ENVIRONMENT', 'DEVELOPMENT');
// define('APP_ENVIRONMENT', 'PRODUCT');
// define('KIS_DEBUG_MODE', 'DISABLE_DEBUG');

/**

*/

// $db_table_prefix = 'shdic_***'; // in func.inc.php


/**



*/


$pagestartime=microtime(); 
$time_log='';

$c='guest';
$a='homepage';
$html_tpl='';
$outVar=array();

$tt = v('c') ;  if($tt) $c=$tt;
$tt = v('a') ;  if($tt) $a=$tt;

if(strlen($c)>=3 && strlen($a)>=3){
    //base check
}else{
    die('403 Access Denied/Forbidden');
}

$debugMode = 0;
$outVar['debugMode']=0;

$tt = intval(v('debug'))  ;
if ($tt>0 ){
    $debugMode=1;
    $outVar['debugMode']=1;
}

$map_uuid = null;
$map_type = null;
$grids = null;
$limit = 0;
$offset = 15;

$tt = intval(v('start'))  ;
if ($tt>0 ){
    $limit=$tt;
}
$tt = intval(v('limit'))  ;
if ($tt>0 && $tt<=500){
    $offset=$tt;
}



$user_key=null;

$html_tpl='demo_tpl';
$html_data='';

switch ($c) {
	case 'test':
        switch ($a) {
    //         case 'frame':
    //             $html_tpl='main_frame_tpl';
    //             $outVar['page_title'] = 'main_frame';
				// $outVar['page_desc'] = 'page_desc';
				// $html_data = '';
    //             break;

    //         case 'render':
    //             $outVar['page_title'] = 'main_frame';
				// $outVar['page_desc'] = 'page_desc';
				// $outVar['html_data'] = 'html_data is nothing';
				// echo render( $outVar , 'web' , 'frame');
				// exit;
    //             break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

	case 'room':
        switch ($a) {
            case 'join':
            // UPDATE `blab8_users` SET `usr_id`=[value-1],`usr_name`=[value-2],`usr_pass`=[value-3],`usr_mail`=[value-4],`usr_join_date`=[value-5],`usr_status`=[value-6],`usr_room`=[value-7] WHERE 1
                $tmp_sql = prepare('UPDATE `'.$blab8_table_prefix.'users` SET `usr_room`=?s WHERE `usr_id`=?s' ,array(0,0,$x1,$x2,$y1,$y2) );
    			// echo $tmp_sql;
    			$ret_map =get_data($tmp_sql);
                break;

    //         case 'render':
    //             $outVar['page_title'] = 'main_frame';
				// $outVar['page_desc'] = 'page_desc';
				// $outVar['html_data'] = 'html_data is nothing';
				// echo render( $outVar , 'web' , 'frame');
				// exit;
    //             break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

	case 'guest':

		$outVar['page_title'] = 'Miss';
		$outVar['page_desc'] = 'page_desc';
		$outVar['html_data'] = 'html_data is nothing';
		$outVar['html_res_root'] = 'html/res/homepage/';	
		$outVar['page_nav'] = '<a href="index.php">首页</a> ';
		$outVar['outVar']=$outVar;

		$html_tpl = 'html/web/' . $a . '.tpl.html';
		if(file_exists($html_tpl)){
			$src = render( $outVar , 'web' , $a);
			$tt = render( $outVar , 'part' , 'headnav');
			$ttf = render( $outVar , 'part' , 'foot');

			$src =str_replace('<!-- $headnav$ -->', $tt, $src);
			echo str_replace('<!-- $foot$ -->', $ttf, $src);			
			exit;
		}else{
			die('403 Access Denied/Forbidden '.$html_tpl);	
		}

		echo render( $outVar , 'part' , 'head');
		echo render( $outVar , 'part' , 'notready');
		echo render( $outVar , 'part' , 'foot');
		exit;

		// break;

	case 'install':
        switch ($a) {
            case 'sql':
                echo init_db_info($db_table_prefix);
				exit;
                break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

    case 'api':
        switch ($a) {
            case 'itmcatjs':
                // $html_data = get_item_cat_js();
                break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

	case 'user':
		switch ($a) {
		case 'index':
			$html_tpl='list_lootset_tpl';
			$outVar['url_para'] = '&pid=0';
			$html_data = '';
			break;

		default:
			die('403 Access Denied/Forbidden '.$c);
		}
		break;
	
	default:
		die('403 Access Denied/Forbidden');
}

/**


*/

if($c=='api' && $action){
    echo $html_data;

}else if($a){

    $time_log.= "页面运行时间: ". used_time($pagestartime) ."秒.<br/>".PHP_EOL; 

	$out = array(
		'ver' => 'v20151110', 
        'app_version' => 'v20151110', 
		'html_res_root' => 'html/res2', 
		'res_root' => 'html/res', 

		'user_key' => $user_key==null?'new':$user_key,
        'data'=> $html_data,

        'time_log'=> $time_log,
        'page_count'=>$offset,

        'js_css_ver'=>'20151110',

        'last_modify_time'=>date('Y-m-d H:i:s'),
		'author' => '492607291#qq.com'
	);
	
    foreach ($outVar as $key => $value) {
        $out[$key] = $value;
    }
	/* Load and display map */
    if(empty($html_tpl)){
        $html_tpl='base_tpl';
    }
	$tbs = template_load($html_tpl);
	$tbs->Show();
}

?>

